<template>
	<div class="main-menu">
		<div class="logo">
			<img class="img" src="@/assets/img/demo1.png" alt=""/>
			<h2 v-show="!isFold" class="title">张三管理平台</h2>
		</div>
		<!--动态菜单-->
		<div class="menu">
			<el-menu v-if="menu.menuList.length!=0" :default-active="menu.defaultSelectActive" :collapse="isFold"
			         text-color="#b7bdc3" active-text-color="#fff" background-color="#001529" unique-opened>
				<template v-for="item in menu.menuList" :key="item.menuId">
					<!--根菜单【没有子菜单】-->
					<el-menu-item v-if="item.children==null || item.children.length==0" :index="item.menuId"
					              @click="handleItemClick(item)">
						<el-icon>
							<component :is="item.menuIcon"></component>
						</el-icon>
						<template #title>{{ item.menuName }}</template>
					</el-menu-item>
					<!--根菜单【包含子菜单】-->
					<el-sub-menu v-else :index="item.menuId">
						<template #title>
							<el-icon>
								<component :is="item.menuIcon"></component>
							</el-icon>
							<span>{{ item.menuName }}</span>
						</template>
						<template v-for="subitem in item.children" :key="subitem.menuId">
							<el-menu-item :index="subitem.menuId" @click="handleItemClick(subitem)">
								{{ subitem.menuName }}
							</el-menu-item>
						</template>
					</el-sub-menu>
				</template>
			</el-menu>
		</div>
	</div>
</template>

<script setup lang="ts">
import { reqGetMenuTree } from '@/service/main/main.ts'
import { errorMessage } from '@/utils/ElMessageUtils.ts'
import { onBeforeMount, reactive } from 'vue'
import router from '@/router'
import { SessionStorageUtils } from '@/utils/StorageUtils.ts'

defineProps({
	isFold: {
		type: Boolean,
		default: false
	}
})

// 动态菜单
let menu = reactive({
	// 无值，则默认选中首页。
	defaultSelectActive: SessionStorageUtils.getKey('menu_default_select_active') ?? '1',
	menuList: []
})
reqGetMenuTree().then(res => {
	// console.log('reqGetMenuTree.res =====>', res)
	if (res.resultStatus) {
		menu.menuList = res.resultData
	} else {
		errorMessage(true, '动态菜单获取失败！！')
	}
})
// console.log("menuList =====>",menuList)


// console.log("SessionStorageUtils =====》 ",SessionStorageUtils.getKey('menu_default_select_active'))

// 路由跳转
function handleItemClick(item: any) {
	// console.log("item ======> ",item)
	// console.log('item.menuPath ==> ', item.menuPath)
	// menu.defaultSelectActive = item.menuId
	SessionStorageUtils.setKey('menu_default_select_active', item.menuId)
	router.push(item.menuPath)
}
</script>

<style scoped lang="less">
.logo {
	display: flex;
	height: 30px;
	padding: 12px 10px 8px 10px;
	margin-bottom: 20px;
	margin-top: 20px;
	flex-direction: row;
	justify-content: flex-start;
	align-items: center;
	overflow: hidden;
	
	.img {
		height: 100%;
		margin: 0 10px;
	}
	
	.title {
		font-size: 16px;
		font-weight: 700;
		color: white;
		white-space: nowrap;
	}
}

.menu {
	.el-menu {
		border-right: none;
		user-select: none;
		
		transition: width 0.3s ease;
	}
	
	.el-menu-item:hover {
		color: #ffffff;
		background-color: #0a60bd;
	}
	
	.el-menu-item.is-active {
		font-size: 17px;
		font-weight: bolder;
		background-color: #0a60bd;
	}
	
	.el-sub-menu.is-active {
		font-size: 17px;
		font-weight: bolder;
		background-color: #0a60bd;
	}
}

//.el-sub-menu {
//	.el-menu-item {
//		padding-left: 50px !important;
//		background-color: #0c2135;
//	}
//	.el-menu-item:hover {
//		color: #ffffff;
//		background-color: #0a60bd;
//	}
//	.el-menu-item.is-active {
//		font-size: 17px;
//		font-weight: bolder;
//		background-color: #0a60bd;
//	}
//}
</style>